<?php
/*
* yoTheme, a html5 wordpress theme.
*
* @licence Apache License
* @author Ethan - http://youed.me - luo123qiu@gmail.com
* 
* Project URL http://code.google.com/p/yotheme/
*/

$ua = $_SERVER['HTTP_USER_AGENT'];
/*
* htmlUA()
*/
function htmlUA($html4,$html5) {
	global $ua;
	if (strpos($ua,'MSIE 8.0')||strpos($ua,'MSIE 7.0')||strpos($ua,'MSIE 6.0')||strpos($ua,'Firefox/3')||strpos($ua,'Firefox/2')||strstr($ua,'Opera')) {
		echo $html4;
	}
	else {
		echo $html5;
	}
}
/* Regist WP menu */
register_nav_menu( 'nav-menu', 'Menu');

/* Include the logo */
include_once(TEMPLATEPATH . '/logo.php');

/**
* register_sidebar()
*
*@desc Registers the markup to display in and around a widget
*/
if ( function_exists('register_sidebar') )
{
  register_sidebar(array(
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  ));
}
/* Language support */
function theme_init(){
	load_theme_textdomain('yotheme', get_template_directory() . '/languages');
}
add_action ('init', 'theme_init');

/* Pagenavi */
function par_pagenavi($range = 9){
	global $paged, $wp_query;
	if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
	if($max_page > 1){if(!$paged){$paged = 1;}
	if($paged != 1){echo "<li><a href='" . get_pagenum_link(1) . "' class='extend' title='".__('Back to the first page', 'yotheme')."'>".__('First', 'yotheme')."</a></li>";}
	echo "<li>";
	previous_posts_link(__('Previous page', 'yotheme'));
	echo "</li>";
    if($max_page > $range){
		if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "<li><a href='" . get_pagenum_link($i) ."'";
		if($i==$paged)echo "";echo ">$i</a></li>";}}
    elseif($paged >= ($max_page - ceil(($range/2)))){
		for($i = $max_page - $range; $i <= $max_page; $i++){echo "</li><a href='" . get_pagenum_link($i) ."'";
		if($i==$paged)echo "";echo ">$i</a></li>";}}
	elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
		for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "<li><a href='" . get_pagenum_link($i) ."'";if($i==$paged) echo "";echo ">$i</a></li>";}}}
    else{for($i = 1; $i <= $max_page; $i++){echo "<li><a href='" . get_pagenum_link($i) ."'";
    if($i==$paged)echo " class='current'";echo ">$i</a></li>";}}
	echo "<li>";
	next_posts_link(__('Next page', 'yotheme'));
	echo "</li>";
    if($paged != $max_page){echo "<li><a href='" . get_pagenum_link($max_page) . "' class='extend' title='".__('Go to the last page', 'yotheme')."'>".__('Last', 'yotheme')."</a></li>";}}
}

/* ShortUrl */
function myUrl($atts, $content = null) {

extract(shortcode_atts(array(

"href" => 'http://'

), $atts));

return '<a target="_blank" href="'.$href.'" rel="nofollow">'.$content.'</a>';

}

add_shortcode("url", "myUrl");

/* Comments */
function yotheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment;
	global $commentcount;
	$page = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
	$cpp=get_option('comments_per_page');
	if(!$commentcount) { 
		if ($page > 1) {
		$commentcount = $cpp * ($page - 1);
		} else {
		$commentcount = 0;
		}
	}
?>

<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
	<div id="comment-<?php comment_ID(); ?>">
		<?php echo get_avatar($comment,$size='32',$default='<path_to_url>' ); ?>
		<div class="cite">
			<strong>
					<?php if (get_comment_author_url()) : ?>
					<a id="commentauthor-<?php comment_ID() ?>" href="<?php comment_author_url(); ?>" target="_blank" title="<?php comment_author(); ?>"><?php comment_author(); ?></a>
					<?php else : ?>
					<span id="commentauthor-<?php comment_ID() ?>"><?php comment_author(); ?></span>
					<?php endif; ?>
			</strong>
			<?php _e(' Says:', 'yotheme'); ?><br />
			<span><?php if(!$parent_id = $comment->comment_parent) {printf(__('#%1$s', 'yotheme'), ++$commentcount);} ?><?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> <a href="#comment-<?php comment_ID() ?>"><?php _e('At', 'yotheme'); ?> <?php comment_date(__('F jS, Y', 'yotheme')) ?> <?php comment_time() ?></a> <?php if(function_exists('mailtocommenter_button')) mailtocommenter_button();?> <?php edit_comment_link(__('(Edit)'),'  ','') ?></span>
		</div>
		<?php if ($comment->comment_approved == '0') : ?>
		<p class="waiting"><?php _e('Your comment is awaiting moderation.') ?></p>
		<?php endif; ?>
		<?php comment_text() ?>
	</div>

<?php } ?>